package com.bw.cinema.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bw.cinema.databinding.ItemSubMutilTypeBinding
import com.bw.cinema.entity.MutilTypeEntity

class SubMutilTypeAdapter(val context: Context, val list: List<MutilTypeEntity>) :
    RecyclerView.Adapter<SubMutilTypeAdapter.SubMutilTypeViewHolder>() {
    class SubMutilTypeViewHolder(val binding: ItemSubMutilTypeBinding) :
        RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SubMutilTypeViewHolder {
        val view = ItemSubMutilTypeBinding.inflate(LayoutInflater.from(context), parent, false)
        return SubMutilTypeViewHolder(view)
    }

    override fun onBindViewHolder(holder: SubMutilTypeViewHolder, position: Int) {
        val entity = list.get(position)

        holder.binding.tvSubMutilTypeCinemaItem.text = entity.typename

        //4
        holder.binding.tvSubMutilTypeCinemaItem.setOnClickListener {
            if (subMutilVideoClickListen != null) {
                subMutilVideoClickListen.click(it, entity)
            }
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    //定义接口 1
    interface SubMutilVideoClickListen {
        fun click(view: View, entity: MutilTypeEntity)
    }

    //定义变量 2
    lateinit var subMutilVideoClickListen: SubMutilVideoClickListen

    // 3
    fun setSubMutilClick(subMutilVideoClickListen: SubMutilVideoClickListen) {
        this.subMutilVideoClickListen = subMutilVideoClickListen
    }
}